<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Service;

/**
 * 谱单服务
 * @author Administrator
 *
 */
class ScoreSheetService{
	
	/**
	 * 新增曲谱
	 */
	public function sheetAdd($score_id = 0 , $sheet_ids){
	    if ($score_id == 0) {
	        E('添加失败');
        }
	    $scoreSheetList = array();
	    foreach ($sheet_ids as $key=>$sheet_id){
	        $scoreSheetList[] = array(
	            'score_id' => $score_id,
	            'sheet_id' => $sheet_id,
	            'creator'  => UID
	        );
	    }

	    $Model = M('ScoreSheet');
	    $result = $Model->addAll($scoreSheetList);

        //谱单曲谱数 +n
        $count = count($scoreSheetList);
        M('Score')->where(array('id'=>$score_id))->setInc('sheet_count', $count);

        return $result;
	}

	/**
     * 获取谱单曲谱数
     */
	public function getSheetCount($scoreIds)
    {
        $model = M('ScoreSheet');
        $map = array(
            'score_id' => array('in', $scoreIds),
            'is_delete' => 0
        );
        $res = $model->field('score_id, count(*)')->where($map)->group('score_id')->select();
        foreach ($res as $key => $val) {
            $sheetCountMap[$val['score_id']] = $val['count(*)'];
        }
        return $sheetCountMap;
    }

    /**
     * 获取曲谱收藏数量(曲谱添加至谱单)
     */
    public function multiFavCount($sheetIds)
    {
        $map = array(
            'sheet_id' => array('in', $sheetIds),
            'is_delete' => 0
        );
        $model = M('ScoreSheet');
        $res = $model->field('sheet_id, count(*)')->where($map)->group('sheet_id')->select();

        $favCountMap = array();
        foreach ($res as $key => $val) {
            $favCountMap[$val['sheet_id']] = $val['count(*)'];
        }
        return $favCountMap;
    }
}